<template>
  <div class="market">
    <van-nav-bar
      title="寄售市场"
      left-text="返回"
      left-arrow
      fixed
      @click-left="$router.back()"
      @click-right="$router.push({ name: 'search' })"
    >
      <template #right>
        <van-icon name="search" size="20" />
      </template>
    </van-nav-bar>

    <van-dropdown-menu active-color="#000000" class="tab">
      <van-dropdown-item v-model="param.type" :options="option1" />
      <van-dropdown-item :title="albumTitle" ref="albumToggle">
        <div class="filter_item">
          <div
            :class="param.album == 0 ? 'on' : 'filter_tag'"
            @click="selectAlbum(null)"
          >
            全部专辑
          </div>
          <div
            :class="param.album == item.id ? 'on' : 'filter_tag'"
            v-for="item in albumList"
            :key="item.id"
            @click="selectAlbum(item)"
          >
            {{ item.name }}
          </div>
        </div>
      </van-dropdown-item>
      <van-dropdown-item v-model="param.sort" :options="option3" />
    </van-dropdown-menu>

    <div class="list">
      <van-pull-refresh
        v-model="refreshloading"
        @refresh="onRefresh"
        class="refresh"
      >
        <van-empty
          v-if="marletList.length < 1 && !loading"
          description="什么都没有"
        />
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了,拉我刷新"
          @load="onLoad"
        >
          <van-cell>
            <div class="goodslist">
              <div
                class="goodsitem"
                v-for="item in marletList"
                :key="item.id"
                @click="
                  $router.push({
                    name: 'marketGoodsDetails',
                    params: { id: item.id },
                  })
                "
              >
                <div class="goodsimage" v-if="setting">
                  <img :src="setting.file_url + item.photo" />
                </div>
                <div class="text">
                  <div class="nameandissunumber">
                    <div class="goodstitle">{{ item.name }}</div>
                    <div class="issunum">
                      <div class="issu">发行</div>
                      <div class="number">{{ item.num }}</div>
                    </div>
                  </div>
                  <div class="goodsprice">
                    <span style="font-weight: lighter">售价:</span>
                    <div style="font-size: 0.3rem">￥{{ item.price }}</div>
                  </div>

                  <div class="goodsprice issuername">
                    <span style="font-weight: lighter; margin-top: 0.1923rem"
                      >发布者:</span
                    >
                    <div class="issuer">
                      <img :src="setting.file_url + item.avatar" alt="" />
                      <div class="name">{{ item.username }}</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </van-cell>
        </van-list>
      </van-pull-refresh>
    </div>
    
    <van-popup class="popup" v-model:show="show" :style="{ height: '8%', width:'40%'}" round>暂未开放</van-popup>
  </div>
</template>

<script setup>
import { computed, ref, watch } from "vue";
//import marketStore from "../store/market";
import albumStore from "../store/album";
import commonStore from "../store/common";

const common = commonStore();
const setting = computed(() => common.setting);
const show = ref(true)
const option1 = [
  { text: "全部藏品", value: 0 },
  { text: "首发", value: 1 },
  { text: "空投", value: 2 },
  { text: "合成", value: 3 },
  { text: "盲盒", value: 4 },
];
const option3 = [
  { text: "最新上架", value: 0 },
  { text: "价格升序", value: "price_desc" },
  { text: "价格倒序", value: "price_asc" },
];

let loading = ref(false);
let finished = ref(false);
let refreshloading = ref(false);
let marletList = ref([]);
// const market = marketStore();
const onLoad = () => {
  // loading.value = true;
  // finished.value = false;
  // refreshloading.value = false;
  // param.value.status = [0, 4];
  // market
  //   .getMarketList(param.value)
  //   .then((res) => {
  //     loading.value = false;
  //     refreshloading.value = false;
  //     if (param.value.page != res.last_page) {
  //       param.value.page++;
  //     } else {
  //       finished.value = true;
  //     }
  //     marletList.value.push(...res.data);
  //   })
  //   .catch(() => {
  //     loading.value = false;
  //     refreshloading.value = false;
  //     finished.value = true;
  //   });
};

const onRefresh = () => {
  marletList.value = [];
  refreshloading.value = true;
  param.value.page = 1;
  onLoad();
};

const album = albumStore();
// album.getAlbumStore();
const albumList = computed(() => album.albumList);

let param = ref({
  page: 1,
  sort: 0,
  album: 0,
  type: 0,
});

const albumTitle = ref("全部专辑");
const albumToggle = ref();
const selectAlbum = (item) => {
  if (!item) {
    param.value.album = 0;
    albumTitle.value = "全部专辑";
  } else {
    param.value.album = item.id;
    albumTitle.value = item.name;
  }
  albumToggle.value.toggle();
};

watch(
  [() => param.value.sort, () => param.value.album, () => param.value.type],
  () => {
    onRefresh();
  }
);
</script>

<style lang="scss" scoped>
.filter_item {
  display: flex;
  padding: 20px;
  padding-left: 10px;

  .filter_tag {
    padding: 10px;
    background-color: #f6f6f6;
    color: black;
    font-weight: bold;
    margin-left: 10px;
    border-radius: 8px;
  }
}
.on {
  background-color: black;
  color: white;
  padding: 10px;
  font-weight: bold;
  margin-left: 10px;
  border-radius: 8px;
}

.tab {
  position: fixed;
  top: 0.8846rem;
  width: 100%;
  z-index: 999;
}

.list {
  width: 100%;
  height: 88%;
  padding-top: 22%;
  padding-bottom: 1.5385rem;
}
.refresh {
  width: 100%;
  height: 100%;
  overflow: auto;
}
.goodslist {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  .goodsitem {
    padding: 0.1923rem;
    background-color: #fff;
    border: solid 0.0288rem #ebeaea;
    margin-bottom: 0.1923rem;

    .goodsimage {
      padding: 0.1923rem;
      background-color: #f7f7f7;

      img {
        width: 2.6rem;
        height: 2.6rem;
      }
    }

    .text {
      display: flex;
      padding: 0.1923rem;
      flex-direction: column;

      .goodstitle {
        font-size: 0.3269rem;
        margin-bottom: 0.0962rem;
        font-weight: bold;
      }

      .goodsprice {
        font-weight: bold;
        display: flex;
        align-items: center;
        justify-content: space-between;
        bottom: 0.1923rem;
        font-size: 0.2885rem;
        color: black;

        span {
          color: #999999;
        }

        .issuer {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-top: 0.1923rem;
        }

        img {
          height: 0.3846rem;
          width: 0.3846rem;
          margin-right: 0.0962rem;
          border-radius: 50%;
        }
      }

      .nameandissunumber {
        .issunum {
          display: flex;
          align-items: center;
          font-size: 0.2rem;
          margin-top: 0.1923rem;
          margin-bottom: 0.1923rem;

          .issu {
            background: black;
            color: #fff;
            display: flex;
            justify-content: center;
            padding: 0 0.05rem;
            height: 0.38rem;
            border: 0.02rem solid black;
            border-top-left-radius: 0.05rem;
            border-bottom-left-radius: 0.05rem;
          }

          .number {
            background: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            border-top-right-radius: 0.05rem;
            border-bottom-right-radius: 0.05rem;
            border: 0.02rem solid black;
            padding: 0 0.05rem;
            height: 0.38rem;
          }
        }
      }
    }
  }
}
.van-cell {
  padding: 0;
  margin-bottom: 10px;
}

</style>